package com.hankol.newretail.adapter.details

import android.os.Bundle
import android.view.View
import com.baselibrary.Constants
import com.baselibrary.extension.checkNotNull
import com.baselibrary.extension.checkValue
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ScreenUtils
import com.baselibrary.utils.SpecUtils
import com.baselibrary.utils.TimeUtil
import com.baselibrary.view.VipHeardImgView
import com.baselibrary.view.mediascontentview.MediaLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.newretail.R
import com.hankol.newretail.bean.EvaluationBean
import com.hankol.newretail.bean.goods_details.GoodsDetails
import com.hankol.newretail.widget.EvaluationRatingBar

/**
 * @author: yuyang
 * @date: 2022/9/5
 * @Des：评价
 */
class GoodsAppraiseAdapter : BaseQuickAdapter<EvaluationBean, BaseViewHolder>(R.layout.item_goods_appraise) {
    private var mGoodsDetail: GoodsDetails? = null
    fun setGoodsDetail(mGoodsDetail: GoodsDetails) {
        this.mGoodsDetail = mGoodsDetail
    }
    private var showReply = false
    fun setShowReplay(showReply: Boolean) {
        this.showReply = showReply
    }

    override fun convert(helper: BaseViewHolder, item: EvaluationBean) {
        if (item.user.checkNotNull()) {
            helper.getView<VipHeardImgView>(R.id.civ_avatar).setData(item.user.icon, item.user.isFans == 1,View.INVISIBLE)
            helper.setText(R.id.tv_name, item.user.name)
            helper.addOnClickListener(R.id.civ_avatar)
            helper.addOnClickListener(R.id.tv_name)
        }
        helper.setText(R.id.tv_time, TimeUtil.fromToday(item.commentTime))
        val ratingBar = helper.getView<EvaluationRatingBar>(R.id.erb_rating_level)
        ratingBar.setStarStyle(12, true, ScreenUtils.dpToPx(2).toFloat(), 4, 12)
        ratingBar.setRatingLevel(item.commentResult, false)
        helper.setText(R.id.tv_goods_scale, SpecUtils.getNormsStr(item.specList))
        if (item.commentContent.checkNotNull()) {
            helper.setGone(R.id.tv_content, true)
            helper.setText(R.id.tv_content, item.commentContent)
        } else {
            helper.setGone(R.id.tv_content, false)
        }

        val nineGridPhoto = helper.getView<MediaLayout>(R.id.photo_content_view)
        if (item.mediaInfoList.checkNotNull()) {
            nineGridPhoto.visibility = View.VISIBLE
            var urlList = ArrayList<String>()
            var typeList = ArrayList<Int>()
            for (media in item.mediaInfoList) {
                typeList.add(media.type)
                if (media.type == 1) {
                    urlList.add(media.videoUrl)
                } else {
                    urlList.add(media.coverUrl)
                }
            }
            nineGridPhoto.setImageUrls(urlList, typeList)
            nineGridPhoto.spacing = ScreenUtils.dpToPx(5).toFloat()
            nineGridPhoto.setCallback { position -> //图片详情
                mGoodsDetail?.let {
                    SensorsDataManager.instance.productEvaluateClick(it.spuId.toString(), if (it.shopInfo == null) "酣客总社" else it.shopInfo.name.checkValue(), it.name.checkValue(),SpecUtils.getNormsStr(item.specList),item.cid)
                }
                val bundle = Bundle()
                bundle.putStringArrayList(Constants.KEY_URLS, urlList)
                bundle.putInt(Constants.KEY_INDEX, position)
                bundle.putStringArrayList(Constants.KEY_COMMENT_CONTENT, arrayListOf(item.commentContent))
                bundle.putStringArrayList(Constants.KEY_COMMENT_TITLE, arrayListOf())
                ActivityRouter.startActivity(ActivityRouter.WINE_CELLAR.IMG_VIDEO_DETAIL, bundle)
            }
        } else {
            nineGridPhoto.visibility = View.GONE
        }
        if (showReply) {
            if (item.ownerReplyList.checkNotNull()) {
                helper.setGone(R.id.ll_shop_reply, true)
                if (item.ownerReplyList[0].user.checkNotNull()) {
                    helper.getView<VipHeardImgView>(R.id.civ_shop_avatar).setData(item.ownerReplyList[0].user.icon, item.user.isFans == 1)
                    helper.setText(R.id.tv_shop_name, item.ownerReplyList[0].user.name)
                    helper.setText(R.id.tv_shop_time, TimeUtil.fromToday(item.ownerReplyList[0].replytime))
                    helper.setText(R.id.tv_shop_content, item.ownerReplyList[0].content)
                    helper.addOnClickListener(R.id.civ_shop_avatar)
                    helper.addOnClickListener(R.id.tv_shop_name)
                }

            } else {
                helper.setGone(R.id.ll_shop_reply, false)
            }
        }
        helper.setGone(R.id.view_bottom_line, helper.adapterPosition != data.size - 1)
    }

}